<?php

/*
 * @author banban
 * @datetime 2020-8-24 19:15:08
 * @copyright 板板<249366615@qq.com>
 * @filename CustomsController
 * 海关查验
 */

namespace App\Controller\Shop;

use App\Model\PssModel\Customs;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Service\TaskServer\TaskWorker;
use App\Service\AppService\SystemCacheConfigService;
use Service\AppService\IdService;
class RealtimedataController extends AbstractController 
{
    /**
     * 接收海关抽查商品下单数据
     * @param RequestInterface $request
     * @return number[]|string[]
     */
    public function receive( RequestInterface $request )
    {
        set_time_limit(0);
        $openreq= $request->post('openReq');
        if($openreq){
            $data= json_decode(htmlspecialchars_decode($openreq),true); 
            Customs::instance()->sign($data['orderNo'], $data['sessionID'], $data['serviceTime']);
        }
        
        return [
            'code'          =>  10000,
            'message'       =>  '',
            'serviceTime'   => MsecTime()
        ];
    }
    
    /**
     * 接收关务回调信通数据提交海关接口
     * @param RequestInterface $request
     */
    public function receivedata(  RequestInterface $request )
    {
        set_time_limit(0);
        $receivedata = $request->post('message');
        logger('info','shopcheck')->info($receivedata);
        //$data = htmlspecialchars_decode($receivedata);
        $data   = $receivedata;
        $receivexml =  simplexml_load_string($data);
        if( $receivexml->SignReturn->returnStatus == 4 )
        {
            $signResult = $receivexml->SignReturn->signResult;
            $copNo = $receivexml->SignReturn->copNo;
            $save =  array('status'=>2,'sign'=>(string)$signResult);
            Customs::instance()->edit(array('orderNo'=>$copNo), $save);
            
            $Realtimedata       = Customs::instance()->info(array('orderNo'=>$copNo));
            $customsconfig      = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('custom');
            $datas              = json_decode($Realtimedata['signdata'],true);
            $datas['certNo']    = $customsconfig['certificateNum'];
            $datas['signValue'] = $Realtimedata['sign'];
            $pushdata           = json_encode($datas,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            $parameter = [
                [
                    'tag'       => 'uploadRealtimeData',
                    'uid'       => IdService::instance()->getOtherID(),
                    'url'       => $customsconfig['upload_url'],
                    'data'      => ['payExInfoStr'=>$pushdata],
                    'method'    => 'post',
                    'format'    => 'x-www-form-urlencoded',
                    'header'    => []
                ]
            ];
            $resTask = TaskWorker::instance()->addTask($parameter);
            $receivedata  = @json_decode(current($resTask)['response']['result'],true);
            if( $receivedata['code']== 10000 )
            {
                Customs::instance()->edit(array('id'=>$Realtimedata['id']), array('status'=>3));
            }
        }
        
    }
}